var app = getApp();
Page({
  data: {
    name: '',
    phone: '',
    location: '所在地区',
    address: '',
    province: '',
    city: '',
    district: '',
    def: true,
    id: ''
  },
  onLoad: function(options) {
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
    var address = JSON.parse(options.address);
    if (address.def==1) {
      address.def = true;
    } else {
      address.def = false;
    }
    this.setData({
      name: address.name,
      phone: address.phone,
      location: address.location,
      address: address.address,
      province: address.province,
      city: address.city,
      district: address.district,
      def: address.def,
      id: address.id
    })
  },
  name: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过6
    if (cursor > 6) {
      this.setData({
        name: value.substring(0, 6)
      })
    } else {
      this.setData({
        name: value
      })
    }
  },
  phone: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    if (isNaN(parseInt(value))) {
      this.setData({
        phone: ''
      })
      return;
    }
    //不能超过11
    if (cursor > 11) {
      this.setData({
        phone: value.substring(0, 11)
      })
    } else {
      this.setData({
        phone: value
      })
    }
  },
  address: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过50
    if (cursor > 50) {
      this.setData({
        address: value.substring(0, 50)
      })
    } else {
      this.setData({
        address: value
      })
    }
  },
  location: function(e) {
    var value = e.detail.value;
    this.setData({
      location: value[0] + '-' + value[1] + '-' + value[2],
      province: value[0],
      city: value[1],
      district: value[2],
    })
  },
  def: function(e) {
    this.setData({
      def: e.detail.value
    })
  },
  addAddress: function() {
    var that = this;
    var name = this.data.name;
    var phone = this.data.phone;
    var location = this.data.location;
    var province = this.data.province;
    var city = this.data.city;
    var district = this.data.district;
    var address = this.data.address;
    var def = this.data.def;
    var id = this.data.id;
    if (name == '') {
      wx.showToast({
        title: '请填写收货人',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (phone == '' && phone.length < 11) {
      wx.showToast({
        title: '请填写正确的手机号码',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (location == '所在地区') {
      wx.showToast({
        title: '请填写所在地区',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (address == '') {
      wx.showToast({
        title: '请填写详细地址',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (def) {
      def = '1'
    } else {
      def = '0';
    }
    wx.showLoading({
      title: '正在修改',
      mask: 2000
    })
    wx.request({
      url: app.globalData.requestUrl + 'updateShippingAddress',
      method: 'POST',
      data: {
        id: id,
        name: name,
        phone: phone,
        province: province,
        city: city,
        district: district,
        address: address,
        def: def,
        userId: app.globalData.user.id
      },
      success: function(r) {
        if (r.data.code == 200) {
          wx.hideLoading();
          var pages = getCurrentPages();
          var page = pages[pages.length - 2];
          page.setData({
            shippingAddressList: r.data.data
          })
          wx.showToast({
            title: '修改成功',
            duration: 2000
          })
          var time = setTimeout(function() {
            clearTimeout(time);
            that.rollback();
          }, 2000)

        } else {
          wx.showToast({
            title: '修改收货地址失败',
            icon: 'none',
            duration: '2000'
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接，修改收货地址失败',
          icon: 'none',
          duration: '2000'
        })
      }
    })
  },
  // 返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  },
})